//= require <framework/w3c/components/Node>

namespace("w3c.Element", {
	'@inherits' : w3c.Node,
	
	getAttribute : function(attrName, element){
		element = element||this.element;
		return element.getAttribute(attrName);
	},
	
	setAttribute : function(attrName, attrValue, element){
		element = element||this.element;
		return element.setAttribute(attrName, attrValue);
	},
	
	removeAttribute : function(){
		element = element||this.element;
		return element.removeAttribute(attrName);
	},
	
	getAttributeNode : function(){
		throw new Error("w3c.Element#getAttributeNode() not yet implemented");
	},
	
	setAttributeNode : function(){
		throw new Error("w3c.Element#setAttributeNode() not yet implemented");
	},
	
	removeAttributeNode : function(){
		throw new Error("w3c.Element#removeAttributeNode() not yet implemented");
	},
	
	getElementsByTagName : function(tagname, element){
		element = element||this.element;
		return element.getElementsByTagName(tagname);
	},
	
	normalize : function(){
		throw new Error("w3c.Element#normalize() not yet implemented");
	},
	
	getElementsByAttribute: function(strTagName, strAttributeName, strAttributeValue){
   	    var oElm = this.element;
    	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    	var arrReturnElements = new Array();
    	var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null;
    	var oCurrent;
    	var oAttribute;
    	for(var i=0; i<arrElements.length; i++){
    		oCurrent = arrElements[i];
    		oAttribute = oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName);
    		if(typeof oAttribute == "string" && oAttribute.length > 0){
    			if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){
    				arrReturnElements.push(oCurrent);
    			}
    		}
    	}
    	return arrReturnElements;
    },
	
	hasChildNode : function (_child, _parent) {
		_parent = _parent||this.element;
		if (_parent === _child) { return false; }
	      while (_child && _child !== _parent) { _child = _child.parentNode; }
	   return _child === _parent;
	},
	
	insertBefore: function(newNode, refNode){
		var el = refNode||this.element;
		return el.parentNode.insertBefore(newNode, el);
    },
	
	insertAfter : function(newNode, refNode) {
		var el = refNode||this.element;
		return el.parentNode.insertBefore(newNode, this.nextSibling(el));
	},
	
	swapNode : function(b) {
	    var a = this.element;
	    var t = a.parentNode.insertBefore(document.createTextNode(""), a);
	    b.parentNode.insertBefore(a, b);
	    t.parentNode.insertBefore(b, t);
	    t.parentNode.removeChild(t);
	    return this;
	}
});
